//
//  BADTabBarViewController.m
//  BAD
//
//  Created by Can on 2018/5/14.
//  Copyright © 2018年 hwd. All rights reserved.
//

#import "BADTabBarViewController.h"
#import "HWDNavigationController.h"
#import "BADTabBar.h"
#import "BADMenuViewController.h"
#import "BADScanViewController.h"

@interface BADTabBarViewController ()<UITabBarControllerDelegate>

//纪录上次的item
@property (nonatomic,strong) UITabBarItem *lastItem;
//是否要跳页的状态，默认值给NO
@property (nonatomic,assign) CGFloat itemState;

@end

@implementation BADTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.delegate = self;
    
    [self setUpAllChildVc];

//    //创建自己的tabbar，然后用kvc将自己的tabbar和系统的tabBar替换下
//    BADTabBar *tabbar = [[BADTabBar alloc] initWithFrame:CGRectMake(0, 0, SCREENW/3, 49)];
//    //kvc实质是修改了系统的_tabBar
//    [self setValue:tabbar forKeyPath:@"tabBar"];
}

- (void)setUpAllChildVc
{
    BADMenuViewController *MenuVC = [[BADMenuViewController alloc] init];
    [self setUpOneChildVcWithVc:MenuVC Image:@"menu01" selectedImage:@"menu01" title:@"Menu"];
    MenuVC.tabBarItem.tag = 1;
    
    BADScanViewController *ScanVC = [[BADScanViewController alloc] init];
    [self setUpOneChildVcWithVc:ScanVC Image:@"nemu02_s" selectedImage:@"nemu02" title:@"Anti-counterfeiting"];
    ScanVC.tabBarItem.tag = 2;
}

#pragma mark - 初始化设置tabBar上面单个按钮的方法

/**
 *  @author li bo, 16/05/10
 *
 *  设置单个tabBarButton
 *
 *  @param Vc            每一个按钮对应的控制器
 *  @param image         每一个按钮对应的普通状态下图片
 *  @param selectedImage 每一个按钮对应的选中状态下的图片
 *  @param title         每一个按钮对应的标题
 */
- (void)setUpOneChildVcWithVc:(UIViewController *)Vc Image:(NSString *)image selectedImage:(NSString *)selectedImage title:(NSString *)title
{
    HWDNavigationController *nav = [[HWDNavigationController alloc] initWithRootViewController:Vc];
    
    Vc.view.backgroundColor = [UIColor whiteColor];
    
    UIImage *myImage = [UIImage imageNamed:image];
    myImage = [myImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    //tabBarItem，是系统提供模型，专门负责tabbar上按钮的文字以及图片展示
    Vc.tabBarItem.image = myImage;
    UIImage *mySelectedImage = [UIImage imageNamed:selectedImage];
    mySelectedImage = [mySelectedImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    Vc.tabBarItem.selectedImage = mySelectedImage;
    Vc.tabBarItem.title = title;
    
    NSMutableDictionary *dictNormal = [NSMutableDictionary dictionary];
    dictNormal[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#757575"];

    NSMutableDictionary *dictSelected = [NSMutableDictionary dictionary];
    dictSelected[NSForegroundColorAttributeName] = [UIColor colorWithHexString:@"#3088fb"];

    [Vc.tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateNormal];
    
    
    if ([Vc isKindOfClass:[BADMenuViewController class]]) {
        [Vc.tabBarItem setTitleTextAttributes:dictNormal forState:UIControlStateSelected];
    }else{
        [Vc.tabBarItem setTitleTextAttributes:dictSelected forState:UIControlStateSelected];
    }
    
    [self addChildViewController:nav];
}

-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{

    if (item.tag == 2) {
        self.itemState = YES;
    }else{
        self.lastItem = item;
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
    if (viewController == self.viewControllers[0]) {
        
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ItemDidClickNotification" object:nil userInfo:nil];
        
        if (self.itemState) {
            self.itemState = NO;
            return YES;
        }
        
        if (viewController.tabBarItem == self.lastItem) {
            return NO;
        }
    }
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
